package com.atguigu01.string.exer2;

import com.sun.nio.sctp.AssociationChangeNotification;
import org.junit.Test;

import java.util.Arrays;

/**
 * ClassName: MyStringAnswers
 * Package: com.atguigu01.string.exer2
 * Description:
 *
 * @Author BuTang
 * @Create 2023/5/22 18:13
 * @Version 1.0
 */
public class MyAnswers {


    /**
     * 模拟trim方法
     */
    @Test
    public void test1() {

        String s = "  abcde  ";

        int isTrue;
        int start = 0;
        int end = s.length() - 1;

        while (true) {
            //int a = s.charAt(start);
            if (' ' == s.charAt(start)) {
                start++;
            }
            if (' ' == s.charAt(end)) {
                end--;
            }
            else if (s.charAt(start+1) != ' ' && s.charAt(end-1) != ' ') break;
        }
        String s1 = s.substring(start,end);
        System.out.println(s1);
    }

    /**
     * 将字符进行反转，比如把abcdefg转换成abcfedg
     * 3,5
     */

    public void reverse(String str,int start,int end){
        char[] c = str.toCharArray();
        for (int i = start , j = end; i < end ; i++,j--) {
            char tmp = c[i];
            c[i] = c[j];
            c[j] = tmp;

        }

        str = String.valueOf(c);
        System.out.println(str);
    }
    @Test
    public void test2(){
        String str = "abcdefg";
        MyAnswers m = new MyAnswers();
        m.reverse(str,3,5);


    }

    /**
     * 题目3：获取一个字符串在另一个字符串中出现的次数。
     *  比如：获取"ab"在 "abkkcadkabkebfkabkskab" 中出现的次数
     */

    public int getCount(String sonstr , String str ){
        int count = 0;
        int i ;
        if (!str.contains(sonstr)) return 0;
        else {
            while (true){
            if (str.contains(sonstr)){
                i = str.indexOf(sonstr);
                count++;
                str = str.substring(i+1);
            } else if (!str.contains(sonstr)) break;
            }
            return  count;
        }
    }
    @Test
    public void test3(){
        MyAnswers m = new MyAnswers();
        System.out.println(m.getCount("ab", "abkkcadkabkebfkabkskab"));
    }

    public String myEquals(String sub , String str){
        if (str.indexOf(sub) < 0 ) {

            String sub1 = null;
            String sub2 = null;
            String sub3 = null;

            int start = 0;
            int end = sub.length();

                while (true) {
                    //sub1 = str.substring(start, end);
                    if (!str.contains(sub)  ){

                        sub1 = sub.substring(start,end);
                        start++;

                        sub2 = sub.substring(start,end);
                        end--;

                        sub3 = sub.substring(start,end);

                        if (str.contains(sub1) ){
                            return sub1;
                        }else if (str.contains(sub2) ){
                            return sub2;
                        }else if (str.contains(sub3) ){
                            return sub3;
                        }
                    }
                    if (start >= end){
                        return null;
                    }
                }
        }

        else return sub;

    }

    @Test
    public void test4(){
        MyAnswers m = new MyAnswers();
        System.out.println(m.myEquals("cvhellobnm", "abcwerthelloyuiodef"));

    }

    public String sore(String str){
        String newstr = null;
        char[] c =str.toCharArray();

        Arrays.sort(c);
        newstr = String.valueOf(c);
        return newstr;
    }
    @Test
    public void test5(){
        MyAnswers m = new MyAnswers();
        String s =m.sore("asjdkadjakdja");
        System.out.println(s);
    }
}
